package com.ctgu.tengyun.service.user.impl;

import com.ctgu.tengyun.bean.request.SysUserBaseInfo;
import com.ctgu.tengyun.dao.mybatis.SysUserMapper;
import com.ctgu.tengyun.domain.SysUser;
import com.ctgu.tengyun.domain.SysUserExample;
import com.ctgu.tengyun.service.user.SysUserService;
import com.ctgu.tengyun.util.StringUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/18.
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper mapper;

    @Override
    public int countByExample(SysUser sysUser) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotEmpty(sysUser.getUserName())){
            criteria.andUserNameLike("%" + sysUser.getUserName() +"%");
        }

        if(StringUtils.isNotEmpty(sysUser.getContactNo())){
            criteria.andContactNoLike("%" + sysUser.getContactNo() +"%");
        }

        return mapper.countByExample(example);
    }

    @Override
    public int insertSelective(SysUser record) {
        return mapper.insertSelective(record);
    }

    @Override
    public List<SysUser> selectByExample(SysUser sysUser, Integer pageNo, Integer pageSize) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotEmpty(sysUser.getUserName())){
            criteria.andUserNameLike("%" + sysUser.getUserName() +"%");
        }

        if(StringUtils.isNotEmpty(sysUser.getContactNo())){
            criteria.andContactNoLike("%" + sysUser.getContactNo() +"%");
        }

        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public int updatePassword(String userName, String oldPassword, String newPassword) {
        SysUser sysUser = new SysUser();
        sysUser.setPassword(newPassword);

        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(userName);
        criteria.andPasswordEqualTo(oldPassword);

        return mapper.updateByExampleSelective(sysUser, example);
    }

    @Override
    public int updateBaseInfo(SysUserBaseInfo baseInfo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(baseInfo, sysUser);
        sysUser.setUserName(null);

        return mapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    public SysUser selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysUser> selectByIds(Collection<Long> ids) {
        if(ids.isEmpty()){
            return Collections.emptyList();
        }
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();

        criteria.andIdIn(Lists.newArrayList(ids));
        return mapper.selectByExample(example);
    }

    @Override
    public SysUser selectByUserName(String userName) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<SysUser> sysUserList = mapper.selectByExample(example);
        if (!sysUserList.isEmpty()) {
            return sysUserList.get(0);
        }

        return null;
    }

    @Override
    public int deleteUserById(Long id) {
        return mapper.deleteUserById(id);
    }
}
